<?php
declare(strict_types=1);

namespace WebApp\lib\db;

use Swoole\Coroutine\Redis;
use Tao\src\util\MyRedis;

/**
 * Class Table
 * @package txz\common\third_lib\model\lib
 */
class HashTable extends AbstractTable
{
    /**
     * @param $server_name
     * @param $dbName
     * @param $tableName
     * @param string $pre
     * @param string $suffix
     * @return mixed
     */
    public function process($server_name, $dbName, $tableName, $pre = '', $suffix = ''): void
    {
        $this->setPre($pre);
        $this->setSuffix($suffix);
        $this->setServerName($server_name);
        $this->setDbName($dbName);
        $this->setTableName(($pre ? $pre . '_' : '') . $tableName . ($suffix ? '_' . $suffix : ''));
    }

    /**
     * @param string $key
     * @return mixed
     */
    public function get(string $key)
    {
        return $this->getPool()->exec(function ($client) use ($key) {
            /** @var Redis $client */
            return $client->get($this->getDbName().':'.$this->getTableName().':'.$key);
        });
    }

    /**
     * @return mixed
     */
    public function hGetAll()
    {
        $all = $this->getPool()->exec(function ($client) {
            /** @var Redis $client */
            return $client->hGetAll($this->getDbName().':'.$this->getTableName());
        });
//        $res = [];
//
//        tracker(I, var_export($all, true));
//
//
//
//        foreach ($all as $index => $val) {
//            if ($index & 1 === 1) {
//                $res[$all[$index - 1]] = $val;
//            }
//        }
//        return $res;
        return $all;
    }

    /**
     * @return mixed
     */
    public function del()
    {
        return $this->getPool()->exec(function ($client) {
            /** @var Redis $client */
            return $client->del($this->getDbName().':'.$this->getTableName());
        });
    }

    /**
     * @param string $key
     * @param string $data
     */
    public function set(string $key, string $data)
    {
        $this->getPool()->exec(function ($client) use ($key, $data) {
            /** @var Redis $client */
            $client->set($this->getDbName().':'.$this->getTableName().':'.$key, $data);
        });
    }

    /**
     * @param string $key
     * @param string $data
     * @param int $time
     */
    public function setEx(string $key, string $data, int $time)
    {
        $this->getPool()->exec(function ($client) use ($key, $data, $time) {
            /** @var Redis $client */
            $client->setex($this->getDbName().':'.$this->getTableName().':'.$key, $time, $data);
        });
    }

    /**
     * @param string $key
     * @return mixed
     */
    public function hGet(string $key)
    {
        return $this->getPool()->exec(function ($client) use ($key) {
            /** @var Redis $client */
            return $client->hGet($this->getDbName().':'.$this->getTableName(), $key);
        });
    }

    /**
     * @param string $key
     * @param string $data
     */
    public function hSet(string $key, string $data)
    {
        $this->getPool()->exec(function ($client) use ($key, $data) {
            /** @var Redis $client */
            $client->hSet($this->getDbName().':'.$this->getTableName(), $key, $data);
        });
    }

    /**
     * @param string ...$key
     * @return mixed
     */
    public function hDel(string ...$key)
    {
        tracker(I, "hDel");
        return $this->getPool()->exec(function ($client) use ($key) {
            /** @var Redis $client */
            return $client->hDel($this->getDbName().':'.$this->getTableName(), ...$key);
        });
    }

    /**
     * @return MyRedis
     */
    public function getPool(): MyRedis
    {
        $key = 'redisPool';
        if (!isset($GLOBALS[$key])) {
            $GLOBALS[$key] = new MyRedis();
        }
        return $GLOBALS[$key];
    }
}
